using GooglePlayGames;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ScorePanel : MonoBehaviour
{
	[SerializeField]
	private AudioClip buttonSound;

	[SerializeField]
	private Text scoreText;

	[SerializeField]
	private Text expText;

	[SerializeField]
	private string leaderboardId;

	[SerializeField]
	private string scoreSaveId;

	private int score;

	private RectTransform rTransform;

	private void Start()
	{
		JavaMessageHandler.OnRewardReceived = (Action<int>)Delegate.Combine(JavaMessageHandler.OnRewardReceived, new Action<int>(OnRewardReceived));
		rTransform = GetComponent<RectTransform>();
		rTransform.anchoredPosition = new Vector2(0f, -400f);
		LeanTween.move(rTransform, new Vector2(0f, 10f), 0.3f).setEase(LeanTweenType.easeOutCubic).setIgnoreTimeScale(useUnScaledTime: true);
		GetResults();
	}

	public void OnRewardReceived(int reward)
	{
		PlayerPrefs.SetInt("Exp", PlayerPrefs.GetInt("Exp") + reward);
		PlayerPrefs.Save();
		SetExpText(score / 10 + reward);
		JavaMessageHandler.OnRewardReceived = (Action<int>)Delegate.Remove(JavaMessageHandler.OnRewardReceived, new Action<int>(OnRewardReceived));
	}

	private void SetExpText(int exp)
	{
		expText.text = "Exp +" + exp + " | Current Exp " + PlayerPrefs.GetInt("Exp");
	}

	private void GetResults()
	{
		score = Mathf.FloorToInt(Mathf.Min(Mathf.Min(0f - StateManager.scr2, StateManager.score), 100000f));
		Social.ReportScore(score, leaderboardId, Check);
		if (score > PlayerPrefs.GetInt(scoreSaveId))
		{
			PlayerPrefs.SetInt(scoreSaveId, score);
		}
		PlayerPrefs.SetInt("Matches Played", PlayerPrefs.GetInt("Matches Played") + 1);
		PlayerPrefs.SetInt("Wins", PlayerPrefs.GetInt("Wins") + 1);
		PlayerPrefs.SetInt("Exp", PlayerPrefs.GetInt("Exp") + score / 10);
		scoreText.text = score.ToString();
		SetExpText(score / 10);
		AchievementsManager.UpdateState();
		if (score > 9000)
		{
			AchievementsManager.UnlockAchievement("CgkI3_DHnsgeEAIQDA");
		}
		if (score >= 4000)
		{
			AchievementsManager.UnlockAchievement("CgkI3_DHnsgeEAIQCw");
		}
		PlayerPrefs.Save();
	}

	private void Check(bool result)
	{
		if (result)
		{
			UnityEngine.Debug.Log("score submission successful");
		}
		else
		{
			UnityEngine.Debug.Log("score submission failed");
		}
	}

	private void ButtonSound()
	{
		AudioSource.PlayClipAtPoint(buttonSound, Camera.main.transform.position);
	}

	private void NormalizeTime()
	{
		Time.timeScale = 1f;
		Time.fixedDeltaTime = 0.033f;
	}

	public void LoadScene(string scene)
	{
		ButtonSound();
		NormalizeTime();
		SceneManager.LoadSceneAsync(scene);
	}

	public void Restart()
	{
		ButtonSound();
		NormalizeTime();
		SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
	}

	public void Leaderboards()
	{
		ButtonSound();
		PlayGamesPlatform.Instance.ShowLeaderboardUI(leaderboardId);
	}
}
